package com.ruoyi.supplier.manager;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Assert;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.ruoyi.supplier.domain.bo.*;
import com.ruoyi.supplier.domain.vo.*;
import com.ruoyi.supplier.domain.vo.product.*;
import com.ruoyi.supplier.domain.vo.spec.SpecPageQuery;
import com.ruoyi.supplier.service.*;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ProductManager {

    @Autowired
    private IProductService iProductService;

    @Autowired
    private IProductSkuService productSkuService;

    @Autowired
    private IProductAlbumService productAlbumService;

    @Autowired
    private IProductSpecService productSpecService;

    @Autowired
    private IProductSpecValueService productSpecValueService;


    public ProductPageRespVo queryProduct(TableDataInfo<ProductVo> dataInfo) {
        ProductPageRespVo respVo = new ProductPageRespVo();
        respVo.setProductCount(new ProductCountVo());
        ProductListVo productListVo = new ProductListVo();
        productListVo.setTotal(dataInfo.getTotal());
        List<ProductRespVo> rows = Convert.toList(ProductRespVo.class,dataInfo.getRows());
        rows.forEach(r -> {
            ProductSkuBo skuBo = new ProductSkuBo();
            skuBo.setProductId(Math.toIntExact(r.getProductId()));
            List<ProductSkuVo> productSkuVos = productSkuService.queryList(skuBo);
            List<SkuVo> skuVos = Convert.toList(SkuVo.class,productSkuVos);
            r.setSkuList(skuVos);
            ProductAlbumBo albumBo = new ProductAlbumBo();
            albumBo.setProductId(Math.toIntExact(r.getProductId()));
            List<ProductAlbumVo> productAlbumVos = productAlbumService.queryList(albumBo);
            List<ContentImageVo> imageVos = Convert.toList(ContentImageVo.class,productAlbumVos);
            r.setContentImage(imageVos);
        });
        productListVo.setRows(rows);
        respVo.setProductList(productListVo);
        return respVo;
    }

    public ProductDetailVo queryProductDetail(Integer productId) {
        ProductVo productVo = iProductService.queryById(productId);
        ProductDetailVo detailVo = new ProductDetailVo();
        ProductRespVo productRespVo = Convert.convert(ProductRespVo.class, productVo);
        ProductSkuBo skuBo = new ProductSkuBo();
        skuBo.setProductId(productId);
        List<ProductSkuVo> productSkuVos = productSkuService.queryList(skuBo);
        productRespVo.setSkuList(Convert.toList(SkuVo.class,productSkuVos));
        detailVo.setDetail(productRespVo);
        ProductSpecVo productSpec = productSpecService.queryByProductId(productVo.getProductId());
        if(productSpec == null) {
            return detailVo;
        }
        ProductDetailSpecVo specVo = Convert.convert(ProductDetailSpecVo.class,productSpec);
        ProductSpecValueBo valueBo = new ProductSpecValueBo();
        valueBo.setSpecId(specVo.getSpecId());
        List<ProductSpecValueVo> productSpecValueVos = productSpecValueService.queryList(valueBo);
        if(CollectionUtil.isEmpty(productSpecValueVos)){
            return detailVo;
        }
        List<ProductDetailSpecItemsVo> collect = productSpecValueVos.stream().map(v -> {
            ProductDetailSpecItemsVo itemsVo = new ProductDetailSpecItemsVo();
            itemsVo.setItemId(v.getSpecValueId());
            itemsVo.setSpecValue(v.getSpecValue());
            return itemsVo;
        }).collect(Collectors.toList());
        specVo.setSpecItems(collect);
        detailVo.setSpecData(specVo);
        return detailVo;
    }

    public int insertBySaveVo(ProductSaveVo saveVo) {
        ProductBo bo = new ProductBo();
        BeanUtil.copyProperties(saveVo,bo);
        iProductService.insertByBo(bo);
        saveVo.getSpecMany().forEach(spec -> {
            //ProductSpecBo specBo = Convert.convert(ProductSpecBo.class,spec);
            //productSpecService.insertByBo(specBo);
            ProductSkuBo skuBo = Convert.convert(ProductSkuBo.class,spec);
            skuBo.setProductId(bo.getProductId());
            productSkuService.insertByBo(skuBo);
            spec.getAlbum().forEach(album -> {
                ProductAlbumBo albumBo = new ProductAlbumBo();
                albumBo.setProductId(bo.getProductId());
                albumBo.setType(album.getType());
                albumBo.setUrl(album.getUrl());
                productAlbumService.insertByBo(albumBo);
            });
        });
        return 1;
    }

    public int updateBySaveVo(ProductSaveVo saveVo) {
        Assert.notNull(saveVo.getProductId(),"商品id不能为空");
        ProductVo productVo = iProductService.queryById(saveVo.getProductId());
        Assert.notNull(productVo,"商品不存在");
        BeanUtil.copyProperties(saveVo,productVo);
        ProductBo productBo = Convert.convert(ProductBo.class,productVo);
        iProductService.updateByBo(productBo);
        saveVo.getSpecMany().forEach(spec -> {
            ProductSkuBo skuBo = Convert.convert(ProductSkuBo.class,spec);
            skuBo.setProductId(saveVo.getProductId());
            productSkuService.save(skuBo);
            spec.getAlbum().forEach(album -> {
                ProductAlbumBo albumBo = new ProductAlbumBo();
                albumBo.setProductId(saveVo.getProductId());
                albumBo.setType(album.getType());
                albumBo.setUrl(album.getUrl());
                albumBo.setProductAlbumId(album.getProductAlbumId());
                productAlbumService.save(albumBo);
            });
        });
        return 1;
    }

    /**
     * 删除商品
     *
     * @param productId
     * @return
     */
    public Boolean deleteById(Integer productId) {
        boolean flag = iProductService.deleteById(productId);
        if(flag) {
            productSkuService.deleteByProductId(productId);
            productAlbumService.deleteByProductId(productId);
        }
        return flag;
    }

    public Boolean updateProductSkuStatus(Integer productSkuId,Integer status) {
        ProductSkuVo skuVo = productSkuService.queryById(productSkuId);
        skuVo.setStatus(status);
        ProductSkuBo skuBo = Convert.convert(ProductSkuBo.class,skuVo);
        return productSkuService.updateByBo(skuBo);
    }

    public TableDataInfo<ProductSpecVo> specList(SpecPageQuery pageQuery) {
        ProductSpecBo bo = new ProductSpecBo();
        bo.setMerchantId(pageQuery.getMerchantId());
        PageQuery pageQueryTemp = new PageQuery();
        pageQueryTemp.setPageNum(pageQuery.getPageNum());
        pageQueryTemp.setPageSize(pageQuery.getPageSize());
        TableDataInfo<ProductSpecVo> dataInfo = productSpecService.queryPageList(bo, pageQueryTemp);
        dataInfo.getRows().forEach(data -> {
            ProductSpecValueBo valueBo = new ProductSpecValueBo();
            valueBo.setSpecId(Long.valueOf(data.getSpecId()));
            List<ProductSpecValueVo> productSpecValueVos = productSpecValueService.queryList(valueBo);
            data.setItems(productSpecValueVos);
        });
        return dataInfo;
    }

    public String addSpec(ProductSpecVo specVo) {
        ProductSpecBo specBo = new ProductSpecBo();
        specBo.setSpecName(specVo.getSpecName());
        specBo.setSupplierId(specVo.getSupplierId());
        productSpecService.insertByBo(specBo);
        if(CollectionUtil.isNotEmpty(specVo.getItems())){
            specVo.getItems().forEach(item -> item.setSpecId(Long.valueOf(specBo.getSpecId())));
            specVo.getItems().forEach(this::addSpecValue);
        }
        return String.valueOf(specBo.getSpecId());
    }

    public String delSpec(Integer specId) {
        List<Integer> ids = new ArrayList<>();
        ids.add(specId);
        productSpecService.deleteWithValidByIds(ids,false);
        productSpecValueService.deleteBySpecId(specId);
        return Boolean.TRUE.toString();
    }

    public String addSpecValue(ProductSpecValueVo valueVo) {
        ProductSpecValueBo valueBo = new ProductSpecValueBo();
        valueBo.setSpecValue(valueVo.getSpecValue());
        valueBo.setSpecId(valueVo.getSpecId());
        productSpecValueService.insertByBo(valueBo);
        return Boolean.TRUE.toString();
    }

    public String delSpecValue(Integer specValueId) {
        List<Integer> ids = new ArrayList<>();
        ids.add(specValueId);
        productSpecValueService.deleteWithValidByIds(ids,false);
        return Boolean.TRUE.toString();
    }
}
